import getTitle from './getTitle'
import http from './HTTP' //
import api from './api'
import planning from '../utils/routeplanning'
import Toast from '@vant/weapp/toast/toast';
let tableidList = ['605c4767a838e6708442e704', '605c5424cc596258042d5438', '6063528ac7878851bc64c70b']
let tableList = ['船政衙门', '罗星塔公园', '阳光学院']
let table_id = ''
let poiLocation = '' //poi点坐标
let park = false //标识，fasle表示当前不在罗星塔

function judument(lat, lng) {
  for (const key in tableList) {
    if (getTitle.getTitle(lat, lng) == tableList[key]) {
      table_id = tableidList[key]
    }
  }
  poiLocation = `${lat},${lng}` //下面的location
  console.log(poiLocation); //当前定位点
}

function navigation(e, qqmapsdk) {
  let addressArr = [],
    cityArr = [],
    longitudeArr = [],
    latitudeArr = [],
    nameArr = [],
    idArr = [],
    addressList = [],
    cityList = [],
    longitudeList = [],
    latitudeList = [],
    nameList = [],
    idList = [];
  let address, city, lon, latitude, name;
  let Index = e.target.dataset.index; //获取点击的下标
  let secondQuantity = 0 //第二页的个数
  qqmapsdk.reverseGeocoder({
    success: (res) => {
      const {
        lat,
        lng
      } = res.result.location;
      console.log(res.result.location);
      //测试坐标：阳光学院'26.004261,119.449420'
      judument(lat, lng) //返回整合好的坐标字符串和特定的table_id
      //罗星塔公园有两页poi点。超出的第二页另外获取 
      //请求第二页数据
      if (getTitle.getTitle(lat, lng) == '罗星塔公园') {
        park = true
        let parkData = {
          key: 'NMKBZ-SACK6-WFDSR-M7UUJ-Q7OYF-FQB6V',
          keyword: '福州', //关键词
          radius: '2000', //2k米
          location: poiLocation,
          auto_extend: 1, //若搜不到则扩大范围		
          page_index: 2, //页码
          table_id: table_id,
        }
        http.get(api.poiurl, parkData, res => {
          console.log('罗星塔公园2;poi点请求成功', res);
          secondQuantity = res.data.result.data.length
          for (let i = 0; i < res.data.result.data.length; i++) {
            addressList.push(res.data.result.data[i].address)
            cityList.push(res.data.result.data[i].city)
            longitudeList.push(res.data.result.data[i].location.lng)
            latitudeList.push(res.data.result.data[i].location.lat)
            nameList.push(res.data.result.data[i].title)
            idList.push(res.data.result.data[i].ud_id) //获取的id是颠倒的
          }
        }, err => {
          console.log('错误信息,', err);
        })
      }
      //获取第一页数据
      let data = {
        key: 'NMKBZ-SACK6-WFDSR-M7UUJ-Q7OYF-FQB6V',
        keyword: '福州', //关键词
        radius: '2000', //2k米
        location: poiLocation, //测试坐标：菜鸟驿站 '26.004261,119.449420'
        auto_extend: 1, //若搜不到则扩大范围			
        table_id: table_id,
      }
      http.get(api.poiurl, data, res => {
        for (let i = 0; i < res.data.result.data.length; i++) {
          addressArr.push(res.data.result.data[i].address)
          cityArr.push(res.data.result.data[i].city)
          longitudeArr.push(res.data.result.data[i].location.lng)
          latitudeArr.push(res.data.result.data[i].location.lat)
          nameArr.push(res.data.result.data[i].title)
          idArr.push(res.data.result.data[i].ud_id) //获取的id是颠倒的
        }
        //倒序输出
        // idArr = idArr.reverse()
        // nameArr = nameArr.reverse()
        if (park) {
          for (const k in idList) {
            addressArr.unshift(addressList[k]) //添加第二组数据
            cityArr.unshift(cityList[k])
            longitudeArr.unshift(longitudeList[k])
            latitudeArr.unshift(latitudeList[k])
            nameArr.unshift(nameList[k])
            idArr.unshift(idList[k])
          }
        }
        console.log('idArr', idArr);
        console.log('nameArr', nameArr);
        // secondQuantity是第二页数据数量
        for (let j = 0; j < res.data.result.data.length + secondQuantity; j++) {
          if (Index == idArr[j] - 1) {
            address = addressArr[j]
            city = cityArr[j]
            lon = longitudeArr[j]
            latitude = latitudeArr[j]
            name = nameArr[j]
          }
          console.log(name);
        }
        let endAddressInfo = {
          address: address,
          city: city,
          longitude: lon,
          latitude: latitude,
          name: name,
        }
        planning.planning(endAddressInfo)

      }, err => {
        console.log('错误信息,', err);
      })
    }
  })
}
export default {
  navigation
}